/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.mvc.controller.moment;

import com.workcell.workdata.im.annotation.CreateAction;
import com.workcell.workdata.im.annotation.UID;
import com.workcell.workdata.im.constant.Common;
import com.workcell.workdata.im.entity.Moment;
import com.workcell.workdata.im.model.request.MomentRequest;
import com.workcell.workdata.im.model.response.PaginationEntity;
import com.workcell.workdata.im.model.response.ResponseEntity;
import com.workcell.workdata.im.model.vo.MomentVO;
import com.workcell.workdata.im.service.MomentRuleService;
import com.workcell.workdata.im.service.MomentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/moment")
@Api(produces = "application/json", tags = "朋友圈内容接口" )
@Validated
public class MomentController {

	@Resource
	private MomentService momentService;

	@Resource
	private MomentRuleService momentRuleService;

	@ApiOperation(httpMethod = "POST", value = "发布朋友圈")
	@PostMapping(value = "")
	@Validated(CreateAction.class)
	public ResponseEntity<Long> publish(@Valid @RequestBody MomentRequest request, @ApiIgnore @UID Long uid) {
		Moment moment = request.ofMoment();
		moment.setUid(uid);
		momentService.add(moment);
		return ResponseEntity.ok(moment.getId());
	}

	@ApiOperation(httpMethod = "GET", value = "分页获取自己的内容列表")
	@ApiImplicitParam(name = "currentPage", value = "当前页数(初始0)", paramType = "path",  dataTypeClass = Integer.class,example = "0")
	@GetMapping(value = "/list/me/{currentPage}")
	public PaginationEntity<MomentVO> list(@ApiIgnore @UID Long uid, @PathVariable int currentPage) {

		Pageable pageable = PageRequest.of(currentPage, Common.API_PAGE_SIZE, Sort.by("id").descending());
		Page<MomentVO> page = momentService.querySelfPage(uid, pageable);

		return PaginationEntity.make(page);
	}

	@ApiOperation(httpMethod = "GET", value = "分页获取朋友圈内容列表")
	@ApiImplicitParam(name = "currentPage", value = "当前页数(初始0)", paramType = "path",  dataTypeClass = Integer.class,example = "0")
	@GetMapping(value = "/timeline/{currentPage}")
	public PaginationEntity<MomentVO> timeline(@ApiIgnore @UID Long uid, @PathVariable int currentPage) {

		Pageable pageable = PageRequest.of(currentPage, Common.API_PAGE_SIZE);

		Page<MomentVO> page = momentService.queryTimelinePage(uid, pageable);

		return PaginationEntity.make(page);
	}

	@ApiOperation(httpMethod = "GET", value = "获取朋友圈最近5条图片，视频内容")
	@ApiImplicitParam(name = "uid", value = "用户ID", paramType = "path",  dataTypeClass = Integer.class,example = "0")
	@GetMapping(value = "/gallery/{uid}")
	public ResponseEntity<List<MomentVO>> gallery(@PathVariable long uid) {

		List<MomentVO> dataList = momentService.queryListGallery(uid);

		return ResponseEntity.ok(dataList);
	}


	@ApiOperation(httpMethod = "DELETE", value = "删除朋友圈内容")
	@ApiImplicitParam(name = "id", value = "朋友圈内容ID", paramType = "path",  dataTypeClass = Integer.class,example = "0")
	@DeleteMapping(value = "/{id}")
	public ResponseEntity<Void> delete(@PathVariable long id) {

		momentService.delete(id);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "GET", value = "分页获取别人的内容列表")
	@ApiImplicitParam(name = "currentPage", value = "当前页数(初始0)", paramType = "path",  dataTypeClass = Integer.class,example = "0")
	@ApiResponses(value = {@ApiResponse(responseCode = "200" ,description = "OK"),@ApiResponse(responseCode = "403" ,description = "禁止访问")})
	@GetMapping(value = "/list/other/{makerId}/{currentPage}")
	public ResponseEntity<?> list(@ApiIgnore @UID Long uid, @PathVariable Long makerId, @PathVariable int currentPage) {

		/*
		 * 被限制访问朋友圈
		 */
		if (momentRuleService.isBlacked(makerId, uid)) {
			return ResponseEntity.make(HttpStatus.FORBIDDEN.value(),"对方不允许查看");
		}

		Pageable pageable = PageRequest.of(currentPage, Common.API_PAGE_SIZE,Sort.by("id").descending());
		Page<MomentVO> page = this.momentService.queryOtherPage(uid,makerId, pageable);
		return PaginationEntity.make(page);

	}

	@ApiOperation(httpMethod = "GET", value = "获取朋友圈内容详情")
	@ApiImplicitParam(name = "id", value = "朋友圈内容ID", paramType = "path",  dataTypeClass = Integer.class,example = "0")
	@GetMapping(value = "/{id}")
	public ResponseEntity<MomentVO> get(@PathVariable long id, @ApiIgnore @UID Long uid) {
		return ResponseEntity.ok(momentService.findOne(id,uid));
	}

}
